<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\Curl;
use PayPal\Api\Invoice;
class Paypal extends Base{
    private $accessToken='A21AAKd52CIqF9vStXLm1tvzce0K-CoS9VVTfw-WVgExMc2k96FUcaMlaBAimAkSEgmbeyp5M2XeXU4gEvyNaBtWNIW9aiEjw';
    private $urlCode='INV2-YANT-QYCB-PCSG-EXEA';

    private $businessEmail='business@netnewpower.com';
    private $clientId='AcevZnF5mMuHsCpo-H4osDhhnbgtO9lh3b-NdxkbIPhn_UT_9hanp74qMVvCRz4OyRGjMyAsg-O2ihWd';
    private $clientSecret='EK6yHVoFncM4xHBP7snoZll6taREX2NzDHGmBQ7bX_727F-kS8te2eSHLRhlERaQQJB6PkVgWYjEQh-9';

    public function paytmd(){
        
        $url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_HEADER, false );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/x-www-form-urlencoded",
            "Authorization: Basic ".base64_encode($this->clientId. ":" .$this->clientSecret)
        ));
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_POST, true );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_USERPWD, $this->clientId . ":" . $this->clientSecret );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials" );
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    public function paytmd1(){
        $jsondraft='{
                      "detail":{
                              "invoice_number":"#tmd'.time().'",
                              "invoice_date":"2023-03-02",
                              "currency_code":"USD",
                              "note":"Thank you.",
                              "payment_term":{
                                 "due_date":"2023-03-12"
                              }
                           },
                           "invoicer":{
                              "name":{
                                 "given_name":"John Doe"
                              },
                              "address":{
                                 "address_line_1":"1 Main St, San Jose",
                                 "admin_area_1":"CA",
                                 "postal_code":"95131",
                                 "country_code":"US"
                              },
                              "email_address":"'.$this->businessEmail.'",
                              "phones":[
                                 {
                                    "country_code":"001",
                                    "national_number":"4086802093",
                                    "phone_type":"MOBILE"
                                 }
                              ],
                              "website":"website.com",
                              "logo_url":"https:\/\/website.com\/images\/website.png"
                           },
                           "primary_recipients":[
                              {
                                 "billing_info":{
                                    "name":{
                                       "given_name":"John Doe"
                                    },
                                    "address":{
                                       "address_line_1":"1 Main St, San Jose",
                                       "admin_area_1":"CA",
                                       "postal_code":"95131",
                                       "country_code":"US"
                                    },
                                    "email_address":"2540148370@qq.com",
                                    "phones":[
                                       {
                                          "country_code":"086",
                                          "national_number":"13667211357",
                                          "phone_type":"MOBILE"
                                       }
                                    ],
                                    "additional_info_value":"add-info"
                                 },
                                 "shipping_info":{
                                    "name":{
                                       "given_name":"John Doe"
                                    },
                                    "address":{
                                       "address_line_1":"1 Main St, San Jose",
                                       "admin_area_1":"CA",
                                       "postal_code":"95131",
                                       "country_code":"US"
                                    }
                                 }
                              }
                           ],
                           "items":[
                              {
                                 "name":"Yoga Mat",
                                 "description":"Elastic mat to practice yoga.",
                                 "quantity":"1",
                                 "unit_amount":{
                                    "currency_code":"USD",
                                    "value":"50.00"
                                 }
                              }
                           ]
                        }';
        
        $url="https://api-m.sandbox.paypal.com/v2/invoicing/invoices";

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_HEADER, false );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json",
            'Accept: application/json',
            "Authorization: Bearer ".$this->accessToken
        ));
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_POST, true );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $jsondraft );
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public function paytmd2(){
        $url = "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/".$this->urlCode."/send";

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_HEADER, false );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json",
            'Accept: application/json',
            "Authorization: Bearer ".$this->accessToken
        ));
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_POST, true );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode(["send_to_invoicer"=>true]) );
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public function invoiceinfo(){
        
        $url = "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/".$this->urlCode;
        $ch = curl_init ();
          curl_setopt ( $ch, CURLOPT_URL, $url );
          curl_setopt ( $ch, CURLOPT_HEADER, false );
          curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
          "Content-Type: application/json",
          "Authorization: Bearer ".$this->accessToken));
          curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
          curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
          $result = curl_exec($ch);
          curl_close($ch);
          return $result;
    }
    public function payqrcode(){
        
        $url = "https://api-m.sandbox.paypal.com/v2/invoicing/invoices/".$this->urlCode."/generate-qr-code";
        $ch = curl_init ();
          curl_setopt ( $ch, CURLOPT_URL, $url );
          curl_setopt ( $ch, CURLOPT_HEADER, false );
          curl_setopt ( $ch, CURLOPT_HTTPHEADER, array(
          "Content-Type: application/json",
          "Authorization: Bearer ".$this->accessToken));
          curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
          curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
          curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode(["width"=>400,"height"=>400]) );
          $result = curl_exec($ch);
          curl_close($ch);
          $tmd=$result;
          $tmd=substr($tmd,strripos($tmd, 'text/plain')+14);
          $tmd=substr($tmd,0,strrpos($tmd, '='));
          return $tmd;
    }
}